﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Criterion;

namespace RMD.Modal
{
    public class DBEmployeeRepository : IEmployeeRepository
    {
        #region IEmployeeRepository Members

        public IList<Employee> GetEmployees()
        {
            ISession session = NHHelper.GetNHSession();
            ICriteria crit = session.CreateCriteria<Employee>();
            return crit.List<Employee>();
        }

        public void AddEmployee(Employee employee)
        {
            ISession session = NHHelper.GetNHSession();
            ITransaction tx = session.BeginTransaction();
            session.Save(employee);
            //session.Flush();
            tx.Commit();
            session.Close();
        }

        public IList<Employee> SearchEmployeeByName(string name)
        {
            ISession session = NHHelper.GetNHSession();
            ICriteria crit = session.CreateCriteria<Employee>();
            crit.Add(Expression.Like("Name", name, MatchMode.Anywhere));
            return crit.List<Employee>();
        }

        public IList<Employee> SearchEmployeeByNumber(string number)
        {
            ISession session = NHHelper.GetNHSession();
            ICriteria crit = session.CreateCriteria<Employee>();
            crit.Add(Expression.Like("Number", number, MatchMode.Start));
            return crit.List<Employee>();
        }
        #endregion
    }
}
